package com.lc.user.express.ordermanagement;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.CostActivity;
import com.lc.user.express.MainActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.AddrAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.app.AppManager;
import com.lc.user.express.httpserver.GetOrderCancel;
import com.lc.user.express.httpserver.GetOrderConfirm;
import com.lc.user.express.httpserver.GetOrderDelete;
import com.lc.user.express.httpserver.GetOrderInfo;
import com.lc.user.express.httpserver.GetOrderPay;
import com.lc.user.express.model.AddrModel;
import com.lc.user.express.model.OrderModel;
import com.lc.user.express.ordering.OrderInfoCompleteActivity;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.view.MyListView;
import com.lc.user.express.view.dialog.AffirmDialog;
import com.zcx.helper.http.AsyCallBack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/3/2. 订单详情
 */
public class OrderDetailsActivity extends BaseSecondActivity {
    public static OnRefreshView onRefreshView;
    public static final int TYPE_1 = 0x88;//从司机已接单跳转过来
    private RelativeLayout rl_diver;//司机
    private TextView tv_status;//状态
    private TextView tv_public;//底部按钮
    private ListView lv;//地址列表
    private List<AddrModel> list = new ArrayList<>();//地址列表数据
    private AddrAdapter adapter;//地址列表adapter
    private TextView tv_need;
    private int checkInfo = -1;//订单状态
    private String orderid;//订单id
    private String cid;//车型id
    private SimpleDraweeView riv_head;
    private TextView tv_feiyong;
    private String cancel_id;

    private TextView head_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        setTitle("订单详情");
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        head_right= (TextView) findViewById(R.id.head_right);
        initView();
        getOrderInfo();
    }

    private void initView() {

        orderid = getIntent().getStringExtra("orderid");
        cancel_id = getIntent().getStringExtra("orderstatus");

        Log.d(cancel_id, "kkkkkkkkkkkkkkkkkkkkkk");
        Log.d(cancel_id, "kkkkkkkkkkkkkkkkkkkkkk");
        if (cancel_id.equals("-10")) {
           setRightButtonVisibility(View.GONE);
            head_right.setVisibility(View.GONE);
        } else {
            head_right.setVisibility(View.VISIBLE);
            setRightButtonVisibility(View.VISIBLE);
        }


        riv_head = (SimpleDraweeView) findViewById(R.id.riv_head);
        rl_diver = (RelativeLayout) findViewById(R.id.rl_diver);
        tv_need = (TextView) findViewById(R.id.tv_need);
        tv_status = (TextView) findViewById(R.id.tv_status);
        tv_public = (TextView) findViewById(R.id.tv_public);
        tv_feiyong = (TextView) findViewById(R.id.tv_feiyong);
        tv_feiyong.setText("费用详情");
        tv_feiyong.setOnClickListener(this);
        tv_public.setOnClickListener(this);
        lv = (MyListView) findViewById(R.id.lv);
        adapter = new AddrAdapter(cnt, list, false);
        lv.setAdapter(adapter);
        onRefreshView = new OnRefreshView() {
            @Override
            public void onRefresh() {
                getOrderInfo();
            }
        };
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_public:
                OnclickPublic();
                break;

            case R.id.head_right:
                new AffirmDialog(cnt, "您确定要删除该订单吗？", "取消", "确定", R.style.MyDialog) {

                    @Override
                    protected void onYesClick() {
                        deleteOrder();
                    }
                }.show();
                break;
            case R.id.tv_feiyong:
                Intent intent = new Intent(this, CostActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("orderid", orderid);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }

    }

    private void getOrderInfo() {
        new GetOrderInfo(orderid, AppContext.myPreferences.getUid(), new AsyCallBack<GetOrderInfo.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetOrderInfo.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                checkInfo = info.checkinfo;
                cid = info.cid;
                setInfoView(info);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }).execute(cnt);
    }

    private void setInfoView(GetOrderInfo.Info info) {

        ((TextView) findViewById(R.id.tv_driver_name)).setText(info.name);
        ((TextView) findViewById(R.id.tv_car_brand_num)).setText(info.license);
        ((TextView) findViewById(R.id.tv_time)).setText(info.posttime);
        ((TextView) findViewById(R.id.tv_car_model)).setText(info.cartype);
        ((TextView) findViewById(R.id.tv_fee)).setText("费用:" + info.account + "元");
        list.clear();
        list.addAll(info.addresslist);
        setNeedView(info.ismove, info.isbackorder, info.isbackmoney, info.backmoney);
        adapter.updateListView(list);

        //   ImageManager.from(cnt).displayImage(riv_head, info.avatar, R.mipmap.default_pic);
        riv_head.setImageURI(Uri.parse(info.avatar));
        Log.d(info.avatar, "fffffffffffffffffffffffff");
        switch (info.checkinfo) {
            case OrderModel.STATUS_REC_ORDER:
                tv_status.setBackgroundColor(getResources().getColor(R.color.green));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("已接单");
                tv_public.setText("取消订单");
                tv_public.setVisibility(View.VISIBLE);
                break;
            case OrderModel.STATUS_RECEIVE_GOODS:
                tv_status.setBackgroundColor(getResources().getColor(R.color.blue));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("已收货");
                tv_public.setText("确认付款");
                tv_public.setVisibility(View.VISIBLE);
                break;
            case OrderModel.STATUS_PAY:
                tv_status.setBackgroundColor(getResources().getColor(R.color.blue2));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("已付款");
                tv_public.setVisibility(View.GONE);
                break;
            case OrderModel.STATUS_CANCEL:
                setTitle("订单详情");
                rl_diver.setVisibility(View.GONE);
                tv_public.setText("再次下单");
                tv_public.setVisibility(View.VISIBLE);
                setRightButtonText("删除订单", 150);
                setNeedView(info.ismove, info.isbackmoney, info.isbackorder, info.backmoney);
                break;
            case OrderModel.STATUS_SENDING_GOODS:
                tv_status.setBackgroundColor(getResources().getColor(R.color.orange));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("送货中");
                tv_public.setText("确认收货");
                tv_public.setVisibility(View.GONE);
                break;
            case OrderModel.STATUS_SENDING_ORDER:
                tv_status.setBackgroundColor(getResources().getColor(R.color.green));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("派单中");
                setTitle("正在派单");
                rl_diver.setVisibility(View.VISIBLE);
                tv_public.setText("取消订单");
                tv_public.setVisibility(View.VISIBLE);
                tv_need.setVisibility(View.VISIBLE);
                setNeedView(info.ismove, info.isbackmoney, info.isbackorder, info.backmoney);

                break;
            case OrderModel.STATUS_ARRIVED:
                tv_status.setBackgroundColor(getResources().getColor(R.color.blue3));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("已到达");
                tv_public.setText("评价");
                break;
            case OrderModel.STATUS_FINISHED:
                tv_status.setBackgroundColor(getResources().getColor(R.color.blue4));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("已完成");
                tv_public.setText("评价");
                tv_public.setVisibility(View.VISIBLE);
                break;
            case OrderModel.STATUS_COMMENTED:
                tv_status.setBackgroundColor(getResources().getColor(R.color.blue5));
                tv_status.setTextColor(getResources().getColor(R.color.white));
                tv_status.setText("已评价");
                tv_public.setVisibility(View.GONE);
                break;
        }

    }

    private void setNeedView(int ismove, int isbackmoney, int isbackorder, String backmoney) {
        Log.i("ismove1111", String.valueOf(ismove));
        if (ismove == 0 && isbackorder == 0 && isbackmoney == 0) {
            tv_need.setText("暂无");
        } else if (ismove == 1 && isbackorder == 0 && isbackmoney == 0) {
            tv_need.setText("需要搬运");
        } else if (ismove == 1 && isbackorder == 1 && isbackmoney == 0) {
            tv_need.setText("需要搬运、免费带回执单");
        } else if (ismove == 1 && isbackorder == 1 && isbackmoney == 1) {
            tv_need.setText("需要搬运、免费带回执单、需带回款" + backmoney + "元");
        } else if (ismove == 0 && isbackorder == 1 && isbackmoney == 0) {
            tv_need.setText("免费带回执单");
        } else if (ismove == 0 && isbackorder == 1 && isbackmoney == 1) {
            tv_need.setText("免费带回执单、需带回款" + backmoney + "元");
        } else if (ismove == 0 && isbackorder == 0 && isbackmoney == 1) {
            tv_need.setText("需带回款" + backmoney + "元");
        }
    }

    private void OnclickPublic() {
        switch (checkInfo) {
            case OrderModel.STATUS_SENDING_GOODS:

            case OrderModel.STATUS_ARRIVED:
//                orderConfirm(orderid);
                Bundle bundle2 = new Bundle();
                bundle2.putString("orderid", getIntent().getStringExtra("orderid"));
                show(CommentActivity.class, bundle2);

                break;
            case OrderModel.STATUS_SENDING_ORDER:
                showCancelOrder();
                break;
            case OrderModel.STATUS_REC_ORDER:
                showCancelOrder();
                break;
            case OrderModel.STATUS_CANCEL:
                Bundle bundle = new Bundle();
                bundle.putSerializable("addrs", (Serializable) list);
                bundle.putString("cid", cid);
                bundle.putString("car_name", ((TextView) findViewById(R.id.tv_car_model)).getText().toString().trim());
                show(OrderInfoCompleteActivity.class, OrderInfoCompleteActivity.TYPE_REORDER, bundle);
                break;
            case OrderModel.STATUS_RECEIVE_GOODS:
                orderPay(getIntent().getStringExtra("orderid"));
                break;
            case OrderModel.STATUS_FINISHED:
                Bundle bundle1 = new Bundle();
                bundle1.putString("orderid", getIntent().getStringExtra("orderid"));
                show(CommentActivity.class, bundle1);
                break;

        }
    }

    private void showCancelOrder() {
        new AffirmDialog(cnt, "您确定要取消该订单吗？", "取消", "确定", R.style.MyDialog) {

            @Override
            protected void onYesClick() {
                // TODO Auto-generated method stub
                cancelOrder();
            }
        }.show();
    }

    /**
     * 取消订单
     */
    private void cancelOrder() {
        new GetOrderCancel(AppContext.myPreferences.getUid(), getIntent().getStringExtra("orderid"), new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);
                if (getIntent().getFlags() == TYPE_1) {//从司机已接单跳转过来
                    show(MainActivity.class);
                    sendBroadcast(new Intent(MainActivity.SWICH_FRAGMENT));
                    finish();
                } else {
                    if (OrderManagementFragment.onRefreshOrderData != null) {
                        OrderManagementFragment.onRefreshOrderData.onRefresh();
                    }
                    AppManager.getAppManager().finishActivity(OrderSendingDetailsActivity.class);
                    finish();
                }

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(cnt, toast);
            }
        }).execute(cnt);//调接口
    }

    private void deleteOrder() {
        new GetOrderDelete(AppContext.myPreferences.getUid(), getIntent().getStringExtra("orderid"), new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);
                if (getIntent().getFlags() == TYPE_1) {//从司机已接单跳转过来
                    show(MainActivity.class);
                    sendBroadcast(new Intent(MainActivity.SWICH_FRAGMENT));
                    finish();
                } else {
                    if (OrderManagementFragment.onRefreshOrderData != null) {
                        OrderManagementFragment.onRefreshOrderData.onRefresh();
                    }
                    AppManager.getAppManager().finishActivity(OrderSendingDetailsActivity.class);
                    finish();
                }

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(cnt, toast);
            }
        }).execute(cnt);//调接口
    }

    /**
     * @param orderid 确认收货
     */
    private void orderConfirm(String orderid) {
        new GetOrderConfirm(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);

                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
                finish();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(cnt, toast);
            }
        }).execute(cnt);//调接口
    }

    /**
     * @param orderid 确认支付
     */
    private void orderPay(String orderid) {
        new GetOrderPay(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);

                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
                finish();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(cnt, toast);
            }
        }).execute(cnt);//调接口
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (onRefreshView != null) {
            onRefreshView = null;
        }
    }

    public interface OnRefreshView {
        void onRefresh();
    }
}
